#include "window/Window.hpp"
#include "window/Editor.hpp"

Editor::Editor(CYFile* file){
    this->file = file;

    this->layout = new QVBoxLayout();

    this->splitter = new QSplitter();

    this->imageViewer = new ImageViewer(file);

    this->textWidget = new QWidget();
    this->textLayout = new QVBoxLayout();
    this->indexLabel = new QLabel(QString("%1").arg(this->file->getImage(0).getNotesCount()));

    this->toolBar = new QToolBar();
    this->normalAction = new QAction();
    this->markAction = new QAction();
    this->noteAction = new QAction();

    this->textEditSplitter = new QSplitter(Qt::Vertical);
    this->initialTextEdit = new QTextEdit();
    this->proofreadingTextEdit = new QTextEdit();

    this->buttonLayout = new QHBoxLayout();
    this->addButton    = new QPushButton("添加");
    this->removeButton = new QPushButton("删除");

    this->buttomLayout   = new QHBoxLayout();
    this->previousButton = new QPushButton("上一页");
    this->pageLabel      = new QLabel(QString("%1/%2").arg(this->imageViewer->getIndex() + 1).arg(this->imageViewer->getSize()));
    this->nextButton     = new QPushButton("下一页");

    this->addingNote = false;

    this->previousButton->setDisabled(true);
    this->initialTextEdit->setDisabled(true);
    this->proofreadingTextEdit->setDisabled(true);
    this->removeButton->setDisabled(true);

    this->normalAction->setIcon(QIcon(":/res/text_normal.png"));
    this->markAction->setIcon(QIcon(":/res/text_mark.png"));
    this->noteAction->setIcon(QIcon(":/res/text_note.png"));

    this->normalAction->setToolTip("普通文本");
    this->markAction->setToolTip("标记文本");
    this->noteAction->setToolTip("注释文本");

    this->indexLabel->setAlignment(Qt::AlignCenter);

    this->toolBar->setDisabled(true);

    this->toolBar->addAction(this->normalAction);
    this->toolBar->addAction(this->markAction);
    this->toolBar->addAction(this->noteAction);

    this->textWidget->setLayout(this->textLayout);
    this->textLayout->addWidget(this->indexLabel);
    this->textLayout->addWidget(this->toolBar);
    this->textLayout->addWidget(this->textEditSplitter);
    this->textLayout->addLayout(this->buttonLayout);

    this->buttonLayout->addStretch();
    this->buttonLayout->addWidget(this->removeButton);
    this->buttonLayout->addWidget(this->addButton);
    this->buttonLayout->addStretch();

    this->buttomLayout->addWidget(this->previousButton);
    this->buttomLayout->addStretch();
    this->buttomLayout->addWidget(this->pageLabel);
    this->buttomLayout->addStretch();
    this->buttomLayout->addWidget(this->nextButton);

    this->initialTextEdit->setParent(this->textEditSplitter);
    this->proofreadingTextEdit->setParent(this->textEditSplitter);

    this->imageViewer->setParent(this->splitter);
    this->textWidget->setParent(this->splitter);

    this->layout->addWidget(this->splitter);
    this->layout->addLayout(this->buttomLayout);

    this->setLayout(this->layout);

    QObject::connect(this->addButton, SIGNAL(clicked()), this, SLOT(addNote()));
    QObject::connect(this->removeButton, SIGNAL(clicked()), this, SLOT(removeNote()));

    QObject::connect(this->previousButton, SIGNAL(clicked()), this, SLOT(previousPage()));
    QObject::connect(this->nextButton, SIGNAL(clicked()), this, SLOT(nextPage()));

    QObject::connect(this->imageViewer, SIGNAL(pageChanged()), this, SLOT(updatePage()));
    QObject::connect(this->imageViewer, SIGNAL(focusChanged()), this, SLOT(updateText()));
    QObject::connect(this->imageViewer, SIGNAL(focusChanged()), this, SLOT(updateDelete()));

    QObject::connect(this->initialTextEdit, SIGNAL(textChanged()), this, SLOT(saveInitialText()));
    QObject::connect(this->proofreadingTextEdit, SIGNAL(textChanged()), this, SLOT(saveProofreadingText()));

    QObject::connect(this->imageViewer, SIGNAL(addDone()), this, SLOT(addDone()));

    // QObject::connect(this->imageViewer, SIGNAL(tryRemove()), this, SLOT(remove()));
    // QObject::connect(this->imageViewer, SIGNAL(tryAdd()), this, SLOT(add()));

    QObject::connect(this->normalAction, SIGNAL(triggered()), this, SLOT(mergeNormal()));
    QObject::connect(this->markAction, SIGNAL(triggered()), this, SLOT(mergeMark()));
    QObject::connect(this->noteAction, SIGNAL(triggered()), this, SLOT(mergeNote()));
    
}

Editor::~Editor(){
    delete this->file;
    
    delete this->initialTextEdit;
    delete this->proofreadingTextEdit;
    delete this->indexLabel;
    
    delete this->addButton;
    delete this->removeButton;
    delete this->buttonLayout;

    delete this->textLayout;
    delete this->textWidget;

    delete this->previousButton;
    delete this->pageLabel;
    delete this->nextButton;
    delete this->buttomLayout;

    delete this->imageViewer;

    delete this->splitter;

    delete this->layout;

}

QString Editor::getFileName(){
    return this->file->getFileName();

}

QString Editor::getFilePath(){
    return this->file->getFilePath();

}

CYFile* Editor::getFile(){
    return this->file;

}

ImageViewer* Editor::getImageViewer(){
    return this->imageViewer;

}

void Editor::saveFile(){
    this->file->saveFile();

}

bool Editor::isSaved(){
    return this->file->isSaved();

}

bool Editor::isAddingNote(){
    return this->addingNote;

}

void Editor::reload(){
    this->imageViewer->setIndex(0);
    this->imageViewer->update();

    this->pageLabel->setText(QString("%1/%2").arg(this->imageViewer->getIndex() + 1).arg(this->imageViewer->getSize()));

}

void Editor::updatePage(){
    this->pageLabel->setText(QString("%1/%2").arg(this->imageViewer->getIndex() + 1).arg(this->imageViewer->getSize()));

    if(this->imageViewer->getIndex() == 0){
        this->previousButton->setDisabled(true);

    }else{
        this->previousButton->setDisabled(false);

    }

    if(this->imageViewer->getIndex() == this->imageViewer->getSize() - 1){
        this->nextButton->setDisabled(true);

    }else{
        this->nextButton->setDisabled(false);

    }

}

void Editor::updateText(){
    if(this->imageViewer->getFocusIndex() != 0){
        this->toolBar->setDisabled(false);

        this->initialTextEdit->setDisabled(false);
        this->proofreadingTextEdit->setDisabled(false);
        
        this->initialTextEdit->setText(this->file->getNoteInitialText(this->imageViewer->getIndex(), this->imageViewer->getFocusIndex() - 1));
        this->proofreadingTextEdit->setText(this->file->getNoteProofreadingText(this->imageViewer->getIndex(), this->imageViewer->getFocusIndex() - 1));
        
        this->indexLabel->setText(QString("%1/%2").arg(this->imageViewer->getFocusIndex()).arg(this->file->getImage(this->imageViewer->getIndex()).getNotesCount()));

    }else{
        this->toolBar->setDisabled(true);

        this->initialTextEdit->setDisabled(true);
        this->proofreadingTextEdit->setDisabled(true);

        this->initialTextEdit->setText("");
        this->proofreadingTextEdit->setText("");
        
        this->indexLabel->setText(QString("%1").arg(this->file->getImage(this->imageViewer->getIndex()).getNotesCount()));

    }

}

void Editor::updateDelete(){
    if(this->imageViewer->getFocusIndex() != 0){
        this->removeButton->setDisabled(false);

    }else{
        this->removeButton->setDisabled(true);

    }

    emit focusChanged();

}

void Editor::saveInitialText(){
    if(this->imageViewer->getFocusIndex() != 0){
        this->file->setNoteInitialText(this->imageViewer->getIndex(), this->imageViewer->getFocusIndex() - 1, this->initialTextEdit->toHtml());

    }

}

void Editor::saveProofreadingText(){
    if(this->imageViewer->getFocusIndex() != 0){
        this->file->setNoteProofreadingText(this->imageViewer->getIndex(), this->imageViewer->getFocusIndex() - 1, this->proofreadingTextEdit->toHtml());

    }

}

// void Editor::add(){
//     this->addButton->setDisabled(true);
//     this->imageViewer->add();

// }

void Editor::addDone(){
    this->addButton->setDisabled(false);

    this->addingNote = false;
    emit added();

}

// void Editor::remove(){
//     if(this->imageViewer->getFocusIndex() != 0){
//         this->file->removeNote(this->imageViewer->getIndex(), this->imageViewer->getFocusIndex() - 1);
//         this->imageViewer->setFocusIndex(0);

//         this->update();

//     }

// }

void Editor::mergeNormal(){
    QTextCursor textCursor = this->initialTextEdit->textCursor();

    QTextCharFormat format = QTextCharFormat();
    format.setBackground(QBrush(QColor(0, 0, 0, 0)));
    format.setForeground(QBrush(QColor(0, 0, 0)));
    format.setFontPointSize(11);

    textCursor.mergeCharFormat(format);

}

void Editor::mergeMark(){
    QTextCursor textCursor = this->initialTextEdit->textCursor();

    QTextCharFormat format = QTextCharFormat();
    format.setBackground(QBrush(QColor(255, 255, 0)));
    format.setForeground(QBrush(QColor(0, 0, 0)));
    format.setFontPointSize(11);

    textCursor.mergeCharFormat(format);

}

void Editor::mergeNote(){
    QTextCursor textCursor = this->initialTextEdit->textCursor();

    QTextCharFormat format = QTextCharFormat();
    format.setBackground(QBrush(QColor(0, 0, 0, 0)));
    format.setForeground(QBrush(QColor(255 * 0.5, 255 * 0.5, 255 * 0.5)));
    format.setFontPointSize(10);

    textCursor.mergeCharFormat(format);

}

void Editor::previousPage(){
    this->getImageViewer()->previous();
    emit pageChanged();

}

void Editor::nextPage(){
    this->getImageViewer()->next();
    emit pageChanged();

}

void Editor::addNote(){
    this->addButton->setDisabled(true);
    this->imageViewer->add();

    this->addingNote = true;
    emit adding();

}

void Editor::removeNote(){
    if(this->imageViewer->getFocusIndex() != 0){
        this->file->removeNote(this->imageViewer->getIndex(), this->imageViewer->getFocusIndex() - 1);
        this->imageViewer->setFocusIndex(0);

        this->update();

    }

}
